package priv.pfz.sbv.spring;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import priv.pfz.sbv.CreateBeanOpData;
import priv.pfz.sbv.CreateBeanOpType;

/**
 * @author yyjxp
 * 2024/2/15 1:00
 */
public class SbvBeanFactory extends DefaultListableBeanFactory {

    public SbvBeanFactory() {
        super();
    }

    public SbvBeanFactory(BeanFactory parentBeanFactory) {
        super(parentBeanFactory);
    }

    /**
     * 创建bean的主方法
     */
    @Override
    protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
        CreateBeanOpData.log(beanName, CreateBeanOpType.CREATE_BEGIN);
        Object object = super.createBean(beanName, mbd, args);
        CreateBeanOpData.log(beanName, CreateBeanOpType.CREATE_END);
        return object;
    }

    /**
     * 实例化
     */
    @Override
    protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) {
        CreateBeanOpData.log(beanName, CreateBeanOpType.INSTANTIATE_BEGIN);
        BeanWrapper beanWrapper = super.createBeanInstance(beanName, mbd, args);
        CreateBeanOpData.log(beanName, CreateBeanOpType.INSTANTIATE_END);
        return beanWrapper;
    }

    /**
     * 填充属性
     */
    @Override
    protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) {
        CreateBeanOpData.log(beanName, CreateBeanOpType.POPULATE_BEGIN);
        super.populateBean(beanName, mbd, bw);
        CreateBeanOpData.log(beanName, CreateBeanOpType.POPULATE_END);
    }
}

